CodeIgniter 整合swfupload –swfupload的session丢失,解决思路。
这个方法也是我试验后,可行的方法。
这个文章也是我做出来之后才开始写的。
程序的流程:上传图片后,key的值加1 ($_SESSION['fileid']++),然后把图片名称存入到对应的key里面.
再返回fileid ,程序接收到值再传 fileid 进行 GET 一个图片。
发生的问题的是:swfupload插件上传之后session不能保存在CI里面,其他的页面也无法获取到该session的值。
只有接受的页面才能获取到值。
解决的思路:既然别的页面不能从session里面获取图片的名称。那么就直接传图片地址过去就可以解决了。
下面放代码出来,因为我是用模块化方法设计的,所以我封装了一下。
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
session_start();
}
function index()
{
if (isset($_POST["PHPSESSID"])) {
session_id($_POST["PHPSESSID"]);
}
ini_set("html_errors", "0");
// Check the upload
if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
echo "ERROR:invalid upload";
exit(0);
}
if (!isset($_SESSION["file_info"])) {
$_SESSION["file_info"] = array();
}
//保存信息到 session
if (!isset($_SESSION['file_info'])) $_SESSION['file_info'] = array();
if (!isset($_SESSION['bigfile_info'])) $_SESSION['bigfile_info'] = array();
if (!isset($_SESSION['fileid'])) $_SESSION['fileid'] = 1;
else $_SESSION['fileid']++;
$fileName = md5(rand()*10000000) . ".jpg";
$config['upload_path'] = './upload/image/';
$config['allowed_types'] = '*';
$config['file_name'] = $fileName;
$this->load->library('upload', $config);
// move_uploaded_file($_FILES["Filedata"]["tmp_name"], "./upload/image/img/" . $fileName);
//CI上传图片的函数
if ( ! $this->upload->do_upload('Filedata'))
{
$error = array('error' => $this->upload->display_errors());
$data['img_error'] = $error;
dump($error);
}
else
{
$file_id = $_SESSION['fileid'];
$_SESSION["file_info"][$file_id] = $fileName;
}
$return_file_name = array('fileid'=>$file_id,'filename'=>$fileName );
echo "FILEID:" . json_encode($return_file_name); // Return the file id to the script
}
//显示图片
function thumbnail($file_name)
{
if ($file_name === false) {
header("HTTP/1.1 500 Internal Server Error");
echo "No File Name";
exit(0);
}
if(!file_exists(set_realpath("upload/image/$file_name")))
{
header("HTTP/1.1 404 Not found");
exit(0);
}
else
{
header("Content-type: image/jpeg") ;
header("Content-length: " . filesize(set_realpath("upload/image/$file_name")));
flush();
readfile(set_realpath("upload/image/$file_name"));
exit(0);
}
}
}
?>
大家可以看到返回值的时候我用
$return_file_name = array('fileid'=>$file_id,'filename'=>$fileName );
echo "FILEID:" . json_encode($return_file_name);
组装成了一个字符串 加json 格式的数据了
handlers.js 文件里面我是这样改的。
function uploadSuccess(file, serverData) {
try {
var progress = new FileProgress(file, this.customSettings.upload_target);
if (serverData.substring(0, 7) === "FILEID:") {
var strFileData = serverData.substring(7);
strFileData = eval('('+strFileData+')');
addImage("../../comment/upload/thumbnail/" + strFileData.filename, strFileData.fileid);
progress.setStatus("获取缩略图...");
progress.toggleCancel(false);
} else {
addImage("../../../static/swfupload/images/error.gif", 0);
progress.setStatus("有错误!");
progress.toggleCancel(false);
alert(serverData);
}
} catch (ex) {
this.debug(ex);
}
}
strFileData = eval('('+strFileData+')'); 使用这个方法可以把json样子的字符串,转成真正的json,其实是一个object,这样就可以用 “.”进行访问了。
接受文件名的这个函数上面的已经发了,改成不是从session里获取图片地址,而是直接接受。
好了,一直困扰我的问题解决了。
这js,是我从dedecms里面剥离出来的。
也发下代码吧,这样显的我无私奉献。
<h4 class="header blue">相册内容</h4>
<script type="text/javascript" src="<?php echo $this->config->base_url('static/swfupload/swfupload/swfupload.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->config->base_url('static/swfupload/js/dedeajax2.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->config->base_url('static/swfupload/js/main.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->config->base_url('static/swfupload/js/album.js');?>"></script>
<script type="text/javascript" src="<?php echo $this->config->base_url('static/swfupload/js/handlers.js');?>"></script>
<script type="text/javascript">
var swfu;
window.onload = function () {
swfu = new SWFUpload({
// Backend Settings
upload_url: "<?php echo $this->config->site_url('comment/upload/index');?>",
post_params: {"PHPSESSID": "<?php session_start(); echo session_id(); ?>"},
// File Upload Settings
file_size_limit : "10 MB",
file_types : "*.jpg;*.png;*.gif",
file_types_description : "JPG Images; PNG Image;GIF Image",
file_upload_limit : 0,
// Event Handler Settings - these functions as defined in Handlers.js
// The handlers are not part of SWFUpload but are part of my website and control how
// my website reacts to the SWFUpload events.
swfupload_preload_handler : preLoad,
swfupload_load_failed_handler : loadFailed,
file_queue_error_handler : fileQueueError,
file_dialog_complete_handler : fileDialogComplete,
upload_progress_handler : uploadProgress,
upload_error_handler : uploadError,
upload_success_handler : uploadSuccess,
upload_complete_handler : uploadComplete,
// Button Settings
button_image_url : "<?php echo $this->config->base_url('static/swfupload/images/SmallSpyGlassWithTransperancy_17x18.png');?>",
button_placeholder_id : "spanButtonPlaceholder",
button_width: 180,
button_height: 18,
button_text : '<div class="button" style="background-color:#E5F1CF; height:26px; text-align:center; line-height:26px">上传图片(可多选)</div>',
button_text_style : '.button { font-family: Helvetica, Arial, sans-serif; font-size: 12pt; } .buttonSmall { font-size: 10pt; }',
button_text_top_padding: 0,
button_text_left_padding: 18,
button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
button_cursor: SWFUpload.CURSOR.HAND,
// Flash Settings
flash_url : "<?php echo $this->config->base_url('static/swfupload/swfupload/swfupload.swf');?>",
flash9_url : "<?php echo $this->config->base_url('static/swfupload/swfupload/swfupload_fp9.swf');?>",
custom_settings : {
upload_target : "divFileProgressContainer",
thumbnail_height: 200,
thumbnail_width: 200,
thumbnail_quality: 100
},
// Debug Settings
debug: false
});
};
</script>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right" >上传图片:</label>
<div class="col-sm-6" style="width: 180px; height: 18px; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;" >
<span id="spanButtonPlaceholder" ></span>
</div>
</div>
<div class="space-4"></div>
<div class="form-group">
<label class="col-sm-2 control-label no-padding-right">上传图片:</label>
<div class="col-sm-8">
<div class="col-sm-8">
<div id="divFileProgressContainer" style="height:75px;"></div>
<div id="thumbnails"></div>
</div>
</div>
</div>
<div class="space-4"></div>
然后谢谢大家,大家多多支持我的个人博客!友情链接互换啊!